#!/bin/sh
#desc:Convert video file format
#package:odvision
#type:local

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later
# Isidro Catalan <skarvin@gmail.com>

# Common video module vars
if ! test -f "/etc/opendomo/videoConfVars.conf"; then
   cp /etc/opendomo/videoConfVars.conf.orig /etc/opendomo/videoConfVars.conf
fi
. "/etc/opendomo/videoConfVars.conf"

# Lib config
. "$LIB_CONFIG"
CONF_FILE="$COMMON_CONF_PATH"

# if it doesn't exist, it is created
if ! test -f "$CONF_FILE"; then
   touch "$CONF_FILE"
fi


CONVERSOR_PATH="/usr/bin/ffmpeg"
FILE_TO_CONVERT="$4"
ENCODE_FROM=`echo "$FILE_TO_CONVERT"|awk -F . '{print $NF}'`
DEST_FILE=`echo "$FILE_TO_CONVERT" | sed "s/.$ENCODE_FROM//g"`

#WE PERFORM THE ENCODING (to all formats, if ffmpeg knows them)
$CONVERSOR_PATH -i "$FILE_TO_CONVERT" "$DEST_FILE.mp4" && \
$CONVERSOR_PATH -i "$FILE_TO_CONVERT" "$DEST_FILE.webm"  && \
rm -f "$FILE_TO_CONVERT" &
# We delete the file ONLY if it has been converted

#$CONVERSOR_PATH -i "$FILE_TO_CONVERT" "$DEST_FILE.mpeg" 
#$CONVERSOR_PATH -i "$FILE_TO_CONVERT" "$DEST_FILE.ogg"
#chmod 777 $DEST_FILE

/bin/logevent notice odvision "Video $FILE_TO_CONVERT converted to [$ENCODE_TO]"
